package sf.database.jdbc.sql;

import sf.common.wrapper.Page;
import sf.database.template.CType;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * 根据模板id或模板内容执行sql语句
 * @author sxf
 */
public interface CrudTemplateInf {
    /**
     * 根据sqlOrId执行批量语句
     * @param conn
     * @param sqlOrId
     * @param type
     * @param parameters
     * @return
     */
    int[] executeBatch(Connection conn, String sqlOrId, CType type, List<Map<String, Object>> parameters);

    /**
     * @param conn
     * @param sqlOrId
     * @param type
     * @param parameters
     * @param insertFast
     * @param batchSize
     * @param pkeys
     * @param keyValues
     * @return
     */
    int[] executeBatch(Connection conn, String sqlOrId, CType type, List<Map<String, Object>> parameters, boolean insertFast,
                       int batchSize, List<String> pkeys, List<Map<String, Object>> keyValues);

    /**
     * @param conn
     * @param pkeys
     * @param keyValues
     * @param sqlOrId
     * @param type
     * @param paramters
     * @return
     */
    int insert(Connection conn, List<String> pkeys, Map<String, Object> keyValues, String sqlOrId, CType type, Map<String, Object> paramters);

    /**
     * @param conn
     * @param sqlOrId
     * @param type
     * @param parameters
     * @return
     */
    int execute(Connection conn, String sqlOrId, CType type, Map<String, Object> parameters);

    /**
     * @param conn
     * @param start
     * @param limit
     * @param beanClass
     * @param sqlOrId
     * @param type
     * @param parameters
     * @param <T>
     * @return 返回
     */
    <T> Page<T> selectPage(Connection conn, long start, int limit, Class<T> beanClass, String sqlOrId, CType type,
                           Map<String, Object> parameters);

    /**
     * 最原始的分页方法,使用原生sql实现
     * @param conn
     * @param start        起始位置
     * @param limit        限制数量
     * @param beanClass    返回结果,可以为实体类,Map,List,Object[]
     * @param countSqlOrId 查询总数的语句id
     * @param countType
     * @param countParas   查询总数的参数
     * @param listSqlOrId  分页列表查询语句id,不能包含了start和limit的内容
     * @param listType
     * @param listParas    分页列表查询参数
     * @param <T>          泛型
     * @return 分页结果
     */
    <T> Page<T> selectPageRaw(Connection conn, long start, int limit, Class<T> beanClass, String countSqlOrId, CType countType, Map<String, Object> countParas,
                              String listSqlOrId, CType listType, Map<String, Object> listParas);

    /**
     * @param conn
     * @param sqlOrId
     * @param type
     * @param parameters
     * @return 返回
     */
    Object[] selectArray(Connection conn, String sqlOrId, CType type, Map<String, Object> parameters);

    /**
     * @param conn
     * @param beanClass
     * @param sqlOrId
     * @param type
     * @param parameters
     * @param <T>
     * @return 返回
     */
    <T> List<T> selectList(Connection conn, Class<T> beanClass, String sqlOrId, CType type, Map<String, Object> parameters);

    /**
     * @param conn
     * @param beanClass
     * @param sqlOrId
     * @param type
     * @param paramters
     * @param start
     * @param limit
     * @param <T>
     * @return
     */
    <T> List<T> selectList(Connection conn, Class<T> beanClass, String sqlOrId, CType type, Map<String, Object> paramters, long start, int limit);

    /**
     * @param conn
     * @param beanClass
     * @param sqlOrId
     * @param type
     * @param parameters
     * @param <T>
     * @return 返回
     */
    <T> T selectOne(Connection conn, Class<T> beanClass, String sqlOrId, CType type, Map<String, Object> parameters);

    /**
     * @param conn
     * @param sqlOrId
     * @param type
     * @param parameters
     * @return 返回
     */
    List<Map<String, Object>> select(Connection conn, String sqlOrId, CType type, Map<String, Object> parameters);

    /**
     * @param conn
     * @param ormIt
     * @param returnClass
     * @param sqlOrId
     * @param type
     * @param paramters
     * @param <T>
     */
    <T> void selectIterator(Connection conn, Consumer<Iterable<T>> ormIt, Class<T> returnClass, String sqlOrId, CType type, Map<String, Object> paramters);

    /**
     * @param conn
     * @param ormStream
     * @param returnClass
     * @param sqlOrId
     * @param type
     * @param paramters
     * @param <T>
     */
    <T> void selectStream(Connection conn, Consumer<Stream<T>> ormStream, Class<T> returnClass, String sqlOrId, CType type, Map<String, Object> paramters);
}
